{extend name="/index/base" /}

{block name="content"}
<div class="layui-card">
    <div class="layui-card-header layuiadmin-card-header-auto">
        <h2>修改</h2>
    </div>
    <div class="layui-card-body">
        <form class="layui-form" action="" method="post">

            <div class="layui-form-item">
                <label for="" class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" value="{$data['name']??''}" lay-verify="required" class="layui-input name" placeholder="">
                </div>
            </div>

            <div class="layui-form-item">
                <label for="" class="layui-form-label">标记</label>
                <div class="layui-input-block">
                    <input type="text" name="act" value="{$data['act']??''}" lay-verify="required" class="layui-input act" placeholder="">
                </div>
            </div>

            <div class="layui-form-item">
                <label for="" class="layui-form-label">排序</label>
                <div class="layui-input-block">
                    <input class="layui-input sort" type="text" name="sort" value="{$data['sort']??''}" placeholder="如：1" >
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <input type="hidden" class="id" value="{$data['id']}" />
                    <span type="submit" id="submit" class="layui-btn" lay-submit="" lay-filter="formDemo">确 认</span>
                    <a  class="layui-btn" href="{:URL('admin/position/index')}" >返 回</a>
                </div>
            </div>

        </form>
    </div>
</div>

{/block}


{block name="script"}
<script>
    function checkSort(val, max){
        var regex = /^\d+$/;
        if(regex.test(val)){
            if(val > max || val <1){
                layer.msg("请输入1-124之间的数字！")
                return false;
            }else{
                return true;
            }
        }else{
            layer.msg("请输入1-124之间的数字！");
            return false;
        }
    }

    layui.use(['layer','table','form'],function () {
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var $ = layui.$ //由于layer弹层依赖jQuery，所以可以直接得到
        //默认提交状态为false
        var commitStatus = false;
        //搜索
        form.on('submit(formDemo)', function(data2) {
            var name = $(".name").val()
            var sort = $(".sort").val();
            var act = $(".act").val();
            var id = $(".id").val();

            if(!checkSort(sort, 124)){
                return false;
            }

            var data = {'name':name,'sort':sort,'act':act,'id':id};

            if(commitStatus==false){
                //提交表单后，讲提交状态改为true
                commitStatus = true;
            }else{
                layer.msg('请勿重复提交');
                return false;
            }

            $.ajax({
                type: 'POST',
                url: '{:url("admin/position/update")}',
                data: data,
                success: function (ret) {
                    commitStatus = true;
                    if (ret.code == 200) {
                        layer.msg(ret.msg, {icon: 6, time: 1000}, function () {
                            window.location.href='{:url("admin/position/index")}';
                        });
                    } else {
                        layer.msg(ret.msg);
                    }
                    return false;
                }
            });
        })

        $(".sort").blur(function(){
            checkSort($(this).val(), 124);
        });


    })
</script>
{/block}
